/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.edu.ntu.yyl.controller.article;


import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.article.ArticleComment;
import cn.edu.ntu.yyl.vo.CommentVO;
import cn.edu.ntu.yyl.service.CommentVOService;
import cn.edu.ntu.yyl.service.ArticleService;
import cn.edu.ntu.yyl.service.ArticleShowService;
import cn.edu.ntu.yyl.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleIndexServlet extends HttpServlet {
    @Autowired
    ArticleShowService articleShowService;
    @Autowired
    CommentVOService commentVOService;
    @Autowired
    ArticleService articleService;

    @Autowired
    private CommentService commentService;
    private Article article;

    @RequestMapping("/article")
    public String ArticleView(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //查询文章
        Integer articleid = 0;
        if (request.getParameter("articleid")!=null){
            String aid = request.getParameter("articleid");
            articleid = Integer.valueOf(aid);
            article = articleService.SelectArticleById(articleid);
            List<ArticleComment> articleComments =  commentService.SelectCommentByArticle(article);
            List<CommentVO> commentVOList = new ArrayList<>();
            for (ArticleComment articleComment:articleComments){
                commentVOList.add(commentVOService.Show(articleComment));
            }
            model.addAttribute("articleview",articleShowService.Show(article));
            model.addAttribute("commentShowList", commentVOList);
        }
        return "default/article";
    }
}
